import axios from 'axios';

class Login {

  static in(account, password, loginType, success, fail) {
    if (!account || !password) {
      success({code: '1', msg: '缺少参数'});
      return;
    }
    var ssoLoginUrl = $tool.getPassportUrl();
    var serverUrl = $tool.getServerUrl();
    $.ajax({
      type: 'get',
      url: ssoLoginUrl + '/jsonp/login.json',
      data: {
        username: account,
        password: password,
        service: serverUrl,
        loginType: loginType
      },
      dataType: 'jsonp',
      success: (res) => {
        var code = res.code;
        if (code == '0') {
          regMyLogin(res.clientKey);
        } else {
          success(res);
        }
      },
      error: (res) => {
        fail(res);
      }
    });
    //注入登录信息
    function regMyLogin(clientKey) {
      var openId = $tool.ss.getItem('openId') || '';
      axios.get('login.json?KI4SO_CLIENT_EC=' + clientKey+'&weixin_openId='+openId).then(function (res) {
        var data = res.data;
        success({
          code: data.isLogin ? '0' : '1'
        });
      }).catch(fail);
    }
  }

  /**
   * 发送验证码
   * @param name
   * @param type
   * @param success
   * @param fail
   */
  static sendCode(code, type, partnerId, success, fail) {
    if (!type || !code) {
      success({code: '1', msg: '缺少参数'});
      return;
    }

    var url = null;
    if (type == 'regemail') {//注册邮箱
      url = 'members/register/verEmail.json?email=' + code;
    } else {
      url = 'members/register/sendCode.json?identifier=' + code + '&type=' + type;
      if(type == 'regphone'){
        url += '&partnerId=' + partnerId;
      }
    }

    axios.get(url).then(function (res) {
      if (!res.data) {
        fail({code: -1});
        return;
      }
      var data = res.data || {};
      if (data.code == '0') {
        //设置发送验证码的时间，防止一直发送
        $tool.setCookie(type + '_code', new Date().getTime(), 60000);
      }
      success({
        code: data.code,
        msg: data.msg || data.data
      });
    }).catch(fail);
  }

  /**
   * 可发送验证码的剩余时间
   * @param type
   * @returns {number}
   */
  static getSendLeftTime(type) {
    var oldTime = $tool.getCookie(type + '_code');
    if (oldTime && oldTime > 0) {
      var left = new Date().getTime() - oldTime;
      return left > 60000 ? 0 : Math.floor((60000 - left) / 1000);
    }
    return 0;
  }

  //校验验证码
  static validateCode(name, type, code, success, fail) {
    axios.get('members/register/verCode.json', {
      params: {
        identifier: name,
        code: code
      }
    }).then(function (res) {
      success(res.data);
    }).catch(fail);
  }

  /**
   * 根据企业邮箱获取企业信息
   * @param email 用户邮箱
   * @param needEpmLevel 是否需要职级数据
   * @param success
   * @param fail
   */
  static getRegisterInfo(email, needEpmLevel, success, fail) {
    axios.get('members/register/getPartnerInfo.json', {
      params: {
        email: email,
        isReturnRank: needEpmLevel
      }
    }).then(function (res) {
      success(res.data);
    }).catch(fail);
  }

  //重置密码
  static setPwd(name, pwd, userId, success, fail) {
    if (!name || !pwd) {
      fail({code: '1', msg: '参数不正确'});
      return;
    }
    axios.post('members/register/findPass.json', {
      phone: name,
      pwd: pwd,
      userId: userId || null
    }).then(function (res) {
      var data = res.data;
      success({
        code: data.code,
        msg: data.msg
      });
    }).catch(fail);
  }

  //用户注册
  static register(registerInfo, success, fail) {
    axios.post('members/register/doRegister.json', registerInfo).then(function (res) {
      success(res.data);
    }).catch(fail);
  }

  /**
   * 获取组织信息
   * @param partnerId
   * @param success
   * @param fail
   */
  static getAllOrgs(partnerId, success, fail) {
    var url = null;
    if (partnerId) {//无须登录
      url = 'members/register/getAllOrgs.json?partnerId=' + partnerId;
    } else {
      url = 'org/getAllOrgs.json';
    }
    axios.get(url).then(res => {
      success(res.data);
    }).catch(fail);
  }
}

export default Login;
